package graphic;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;

/**
 *
 * @author Abyx
 */
public class ConnectWindow extends JFrame implements Runnable{

    /**
	 * 
	 */
	private static final long serialVersionUID = 3079938297437198193L;
	private ControlGUI control;
	/**
     * Creates new form ConnectWindow
     */
    public ConnectWindow(ControlGUI cGUI) {
        this.control = cGUI;
    	initComponents();
    	this.connectButton.addActionListener(new CoButListener());
    	this.loginField.addKeyListener(new TextListener());
    }
                
    private void initComponents() {

        connectButton = new JButton();
        loginField = new JTextField();
        loginLabel = new JLabel();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        connectButton.setText("Connect");

        loginLabel.setText("Enter your login here");

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(loginLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(loginField)
                    .addComponent(connectButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(loginLabel)
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(loginField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(connectButton)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        getAccessibleContext().setAccessibleName("Connect");

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    
    public void run() {
    	this.setVisible(true);
    	this.loginField.setEditable(true);
    }
    // Variables declaration - do not modify                     
    private JButton connectButton;
    private JTextField loginField;
    private JLabel loginLabel;
    // End of variables declaration                   
    
    public void processConnect(){
    	this.control.performConnect(loginField.getText());
    }
    
    public void setEdit (boolean editable){
    	this.loginField.setEditable(editable);
    }
    
    private class CoButListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (!control.getConnected()){
				String login = loginField.getText();
				if (login.equals("")){
					loginLabel.setText("Enter a VALID login");
				}
				else {
					control.performConnect(loginField.getText());
					loginLabel.setText("Enter your login here");
				}
			}
		}    	
    }
    
    private class TextListener implements KeyListener {

		@Override
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER){
				if (control.getConnected()==false){
					connectButton.doClick();
				}
			}
		}
		@Override
		public void keyReleased(KeyEvent e) {			
		}
		@Override
		public void keyTyped(KeyEvent e) {
			//if (e.getKeyCode() == KeyEvent.VK_ENTER)
			//sendButton.doClick();
		}    	
    }
    
    
    
    
}
